package DataStructureBook;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;

public class TestOne {
    public static void main(String[] args) {
        Circle[] circles = new Circle[]{new Circle(10), new Circle(20), new Circle(30)};

        Collection<Circle> circles1 = new ArrayList<>();
        circles1.add(new Circle(10));
        circles1.add(new Circle(10));
        circles1.add(new Circle(10));

        System.out.println(totalArea(circles));
        System.out.println(totalArea(circles1));

        String[] arr = {"ZEBRA","alligator","crocodile"};
        String[] arr1 = {"Cd","ba","ca"};
        System.out.println(findMax(arr1,new CaseInsensitiveCompare()));
    }

    public static double totalArea(Shape[] shapes) {
        double total = 0;
        for (Shape s : shapes) {
            if (s != null) {
                total += s.getArea();
            }
        }
        return total;
    }
//不能接受参数Collection<Circle>
//    public static double totalArea(Collection<Shape> shapes) {
//        double total = 0;
//        for (Shape s : shapes) {
//            if (s != null) {
//                total += s.getArea();
//            }
//        }
//        return total;
//    }

    //可以接受参数Collection<Circle>
    public static double totalArea(Collection<? extends Shape> shapes) {
        double total = 0;
        for (Shape s : shapes) {
            if (s != null) {
                total += s.getArea();
            }
        }
        return total;
    }

    public static <AnyType> AnyType findMax(AnyType[] arr, Comparator<? super AnyType> cmp) {
        int maxIndex = 0;
        for (int i = 0, len = arr.length; i < len; i++) {
            if(cmp.compare(arr[i],arr[maxIndex])>0){
                maxIndex = i;
            }
        }
        return arr[maxIndex];
    }
}

class CaseInsensitiveCompare implements Comparator<String>{

    @Override
    public int compare(String o1, String o2) {
        return o1.compareToIgnoreCase(o2);
    }
}